package com.youdoneed.community.shortVideo.service;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import com.github.pagehelper.PageHelper;
import com.youdoneed.base.application.AppException;
import com.youdoneed.community.common.utils.StringUUIdUtil;
import com.youdoneed.community.shortVideo.dao.MusiclibraryMapper;
import com.youdoneed.community.shortVideo.dao.MusicrecommendMapper;
import com.youdoneed.community.shortVideo.dao.MusictypeMapper;
import com.youdoneed.community.shortVideo.dao.MusictyperelMapper;
import com.youdoneed.community.shortVideo.entity.Musiclibrary;
import com.youdoneed.community.shortVideo.entity.Musicrecommend;
import com.youdoneed.community.shortVideo.entity.Musictype;
import com.youdoneed.community.shortVideo.entity.Musictyperel;
import com.youdoneed.community.shortVideo.vo.MusicBatchVo;
import com.youdoneed.community.shortVideo.vo.RecommendVo;

@Service
public class MusiclibraryService {
	
	private final Logger logger = LoggerFactory.getLogger(MusiclibraryService.class);
	
	private final String musicLibraryType = "2aeded1129784decbab8b8bf18329c7c";
	
	@Autowired
	MusiclibraryMapper musiclibraryMapper;
	
	@Autowired
	MusictyperelMapper musictyperelMapper;
	
	@Autowired
	MusicrecommendMapper musicrecommendMapper;
	
	@Autowired
	MusictypeMapper musictypeMapper;
	
	/**
	 * 向音乐库添加音乐
	 * @param musiclibrary
	 * @author lixs
	 * @Date 2017.8.31
	 */
	@Transactional
	public int addMusicToLibrary(Musiclibrary musiclibrary,List<String> typeId){
		if(null==musiclibrary){
			throw new AppException("音乐对象不能为空");
		}
		Musiclibrary musiclibraryNew = new Musiclibrary();
		musiclibraryNew.setMusicId(StringUUIdUtil.getUUID());
		musiclibraryNew.setAlbumCoverAddress(musiclibrary.getAlbumCoverAddress());
		musiclibraryNew.setCreateDate(new Date());
		musiclibraryNew.setMusicName(musiclibrary.getMusicName());
		musiclibraryNew.setPlayCount(0);
		musiclibraryNew.setSinger(musiclibrary.getSinger());
		musiclibraryNew.setSongLength(musiclibrary.getSongLength());
		musiclibraryNew.setMusicAddress(musiclibrary.getMusicAddress());
		musiclibraryMapper.insertSelective(musiclibraryNew);
		if(null!=typeId){
		for(String id:typeId){
			Musictype musictype = musictypeMapper.selectByPrimaryKey(id);
			if(null==musictype){
				logger.info("不存在该音乐分类");
				logger.info("id:"+id);
				continue;
			}
			
			Musictyperel musictyperelNew = new Musictyperel();
			musictyperelNew.setCreateDate(new Date());
			musictyperelNew.setMusicId(musiclibraryNew.getMusicId());
			musictyperelNew.setMusicTypeRelId(StringUUIdUtil.getUUID());
			musictyperelNew.setPlayCount(0);
			musictyperelNew.setTypeId(id);
			musictyperelMapper.insertSelective(musictyperelNew);
			}
		}
		//每向音乐库中添加一首音乐，则向音乐库类型分类下添加一条记录
		Musictyperel musictyperel = new Musictyperel();
		musictyperel.setCreateDate(new Date());
		musictyperel.setMusicId(musiclibraryNew.getMusicId());
		musictyperel.setMusicTypeRelId(StringUUIdUtil.getUUID());
		musictyperel.setPlayCount(0);
		musictyperel.setTypeId(musicLibraryType);
		return musictyperelMapper.insertSelective(musictyperel);
		
	}   
	
	/**
	 * 从音乐库删除音乐记录
	 * @param musicId
	 * @author lixs
	 * @Date 2017.8.31
	 */
	@Transactional
	public int deleteMusicFromLibrary(Musiclibrary musiclibrary){
		if(!StringUtils.hasText(musiclibrary.getMusicId())){
			throw new AppException("音乐id不能为空");
		}
		Musiclibrary musiclibraryNew = musiclibraryMapper.selectByPrimaryKey(musiclibrary.getMusicId());
		if(null==musiclibraryNew){
			throw new AppException("未找到该音乐记录");
		}
		//同时删除各个分类下的相同的音乐
		List<Musictyperel> musictyperel = musictyperelMapper.findMusictyperelByMusicId(musiclibrary.getMusicId());
		if(null!=musictyperel){
			for(int index=0;index<musictyperel.size();index++){
				musictyperelMapper.deleteByPrimaryKey(musictyperel.get(index).getMusicTypeRelId());
			}
		}
		//删除推荐列表中的音乐
		List<Musicrecommend> musicrecommend = musicrecommendMapper.findMusicById(musiclibrary.getMusicId());
		if(null!=musicrecommend){
			for(int index=0;index<musicrecommend.size();index++){
				musicrecommendMapper.deleteByPrimaryKey(musicrecommend.get(index).getRecommendMusicTypeId());
			}
		}
		return musiclibraryMapper.deleteByPrimaryKey(musiclibrary.getMusicId());
	}
	
	
	/**
	 * 修改音乐信息
	 * @param musiclibrary
	 * @author lixs
	 * @Date 2017.8.31
	 */
	@Transactional
	public int updateMusic(Musiclibrary musiclibrary){
		if(null==musiclibrary){
			throw new AppException("音乐记录为空");
		}
		Musiclibrary musiclibraryNew = musiclibraryMapper.selectByPrimaryKey(musiclibrary.getMusicId());
		if(null==musiclibraryNew){
			throw new AppException("未找到该音乐记录");
		}
		musiclibraryNew.setAlbumCoverAddress(musiclibrary.getAlbumCoverAddress());
		musiclibraryNew.setMusicName(musiclibrary.getMusicName());
		musiclibraryNew.setSinger(musiclibrary.getSinger());
		musiclibraryNew.setVideoAddress(musiclibrary.getVideoAddress());
		musiclibraryNew.setSongLength(musiclibrary.getSongLength());
		musiclibraryNew.setMusicAddress(musiclibrary.getMusicAddress());
		return musiclibraryMapper.updateByPrimaryKeySelective(musiclibraryNew);
	}
	
	/**
	 * 多条件查询音乐列表
	 * @param singer
	 * @param songName
	 * @param createDateS
	 * @param createDateE
	 * @param currentPage
	 * @param rows
	 * @author lixs
	 * @Date 2017.8.31
	 */
	public List<RecommendVo> findMusicFromLibrary(String typeId,String name,String createDateS,String createDateE,
												   Integer currentPage,Integer rows){
		PageHelper.startPage(currentPage, rows);
		return musiclibraryMapper.findMusicFromLibrary(typeId,name, createDateS, createDateE);
	}
	
	
	public int findMusicFromLibraryForTotal(String typeId,String name,String createDateS,String createDateE){
		return musiclibraryMapper.findMusicFromLibraryForTotal(typeId,name, createDateS, createDateE);
	}
	
	/**
	 * 查询所有音乐视频
	 * @author lixs
	 * @Date 2017.9.15
	 */
	public List<Musiclibrary> findVideoFromMusiclibrary(){
		
		List<Musiclibrary> list = musiclibraryMapper.findVideoFromMusiclibrary();
		if(null == list){
			throw new AppException("未查到任何音乐视频");
		}
		return list;
	}

	/**
	 * 根据歌手或者歌曲名查询音乐库中的音乐
	 * @param name
	 * @param currentPage
	 * @param rows
	 * @author lixs
	 * @Date 2017.10.23
	 */
	public List<Musiclibrary> findMusiclibraryByName(String name,Integer currentPage,Integer rows){
		PageHelper.startPage(currentPage,rows);
		return musiclibraryMapper.findMusiclibraryByName(name);
	}
	
	public int countMusiclibraryByName(String name){
		return musiclibraryMapper.countMusiclibraryByName(name);
	}
	
	/**
	 * 批量导入音乐(请勿乱用)
	 * @param musicBatchVo
	 * @param urlVo
	 * @param typeId
	 * @author lixs
	 * @Date 2017.11.13
	 */
	@Transactional
	public Boolean addMusicToLibraryBatch(List<MusicBatchVo> musicBatchVo,String typeId){
		if(null==musicBatchVo){
			throw new AppException("音乐属性文件不能为空");
		}
		if(!StringUtils.hasText(typeId)){
			throw new AppException("音乐类型id不能为空");
		}
		
		Musictype musictype = musictypeMapper.selectByPrimaryKey(typeId);
		
		if(null==musictype){
			throw new AppException("未找到该音乐分类");
		}
		
		for(int index=0;index<musicBatchVo.size();index++){
			//向音乐库中添加一条记录
			Musiclibrary musiclibrary = new Musiclibrary();
			musiclibrary.setAlbumCoverAddress(musicBatchVo.get(index).getAlbumCoverAddress());
			musiclibrary.setCreateDate(new Date());
			musiclibrary.setMusicAddress(musicBatchVo.get(index).getMusicAddress());
			musiclibrary.setMusicId(StringUUIdUtil.getUUID());
			musiclibrary.setMusicName(musicBatchVo.get(index).getMusicName());
			musiclibrary.setPlayCount(0);
			musiclibrary.setSinger(musicBatchVo.get(index).getSinger());
			musiclibrary.setSongLength(musicBatchVo.get(index).getSongLength());
			musiclibraryMapper.insertSelective(musiclibrary);
			
			//每次向首页热点音乐模块下添加一条记录
			Musictyperel musictyperel = new Musictyperel();
			musictyperel.setCreateDate(new Date());
			musictyperel.setMusicId(musiclibrary.getMusicId());
			musictyperel.setMusicTypeRelId(StringUUIdUtil.getUUID());
			musictyperel.setPlayCount(0);
			musictyperel.setTypeId(musicLibraryType);
			musictyperelMapper.insertSelective(musictyperel);
			
			//向音乐分类下的模块添加一条记录
			Musictyperel musictyperelNew = new Musictyperel();
			musictyperelNew.setCreateDate(new Date());
			musictyperelNew.setMusicId(musiclibrary.getMusicId());
			musictyperelNew.setMusicTypeRelId(StringUUIdUtil.getUUID());
			musictyperelNew.setPlayCount(0);
			musictyperelNew.setTypeId(typeId);
			musictyperelMapper.insertSelective(musictyperelNew);
		}
		return true;
	}
}
